<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org"
>

<head>

    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">

</head>
<style>
    .cx>div{
        margin-bottom: 10px;
    }
</style>
<body>

<div class="panel panel-default operation_dept" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane" action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
<!--            <input name="aid" hidden/>-->
            <div class="layui-form-item">
                <label class="layui-form-label">用户问题</label>
                <div class="layui-input-block">
                    <input type="question" name="question" placeholder="请输入用户问题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">机器人回答</label>
                <div class="layui-input-block">
                    <input type="answer" name="answer" placeholder="请输入机器人回答" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switch" lay-text="使用|禁用" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="dept-table">
    <!--  ---------------------------------搜索条件  -->
    <div id="searchParam" shiro:hasPermission="sys:user:list">
        <div class="layui-form-item cx">
            <div class="layui-input-inline">
                <input type="text" id="question" class="layui-input" autocomplete="off" placeholder="请输入用户问题">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="answer" class="layui-input" autocomplete="off" placeholder="请输入机器人回答">
            </div>
            <div class="layui-input-inline layui-form">
                <select id="status">
                    <option value="0">请选择状态</option>
                    <option value="1">使用</option>
                    <option value="2">禁用</option>
                </select>
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()" id="search">查询</button>
            </div>
        </div>

    </div>
    <!--  ------------------------------表格  -->
    <table class="layui-table" id="robotTable" lay-filter="robotTable"></table>
</div>
<!--<div id="deptTree" style="display: none"></div>-->
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addDept" shiro:hasPermission="sys:dept:add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="sys:dept:add">删除</button>
<!--        <button class="layui-btn layui-btn-sm" lay-event="downloadAnnual" style="float: right;margin-right: -118px;" shiro:hasPermission="sys:dept:add">下载</button>-->
<!--        <button class="layui-btn layui-btn-sm" lay-event="uploadAnnual" id="uploadAnnual" style="float: right;margin-right: -64px;" shiro:hasPermission="sys:dept:add">上传</button>-->
    </div>

</script>
<script type="text/html" id="tool">
    <a shiro:hasPermission="sys:dept:update" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a shiro:hasPermission="sys:dept:deleted" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns3;
    var uploadIns;
    var table = layui.table;
    var upload = layui.upload;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var tree = layui.tree;
    var transfer = layui.transfer;
    var deptNo = null;
    // var startDateOfEstablishment = null;
    // var endDateOfEstablishment = null;
    layui.use(['table', 'layer', 'laydate', 'tree', 'transfer','upload'], function () {
        tableIns3 = table.render({
            elem: '#robotTable'// 表格id
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sys/robots' //数据接口
            , method: 'POST',
            toolbar: '#toolbar'
            ,defaultToolbar: []
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                console.log(res);
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data.list) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data.list) ? null : res.data.list //解析数据列表
                }
            },
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'question', title: '用户问题'},
                    {field: 'answer', title: '机器人回答'},
                    // {
                    //     field: 'dateOfEstablishment', title: '成立日期',width: 150, templet: function (item) {
                    //         return "<div>"+layui.util.toDateString(item.dateOfEstablishment, 'yyyy年MM月dd日')+"</div>";
                    //     }
                    // },
                    {
                        field: 'status', title: '状态',width: 90, templet: function (item) {
                            if (item.status === 1 || item.status == "1") {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="使用|禁用" checked disabled>';
                            }
                            if (item.status === 2 || item.status == "2") {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="使用|禁用" disabled>';
                            }
                        }
                    },
                    {title: '操作', width: 250, toolbar: '#tool'}
                ]
            ]
        });

        //操作项事件
        table.on('tool(robotTable)', function (obj) {
            console.log(obj,111);
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var annualIds = [];
                    annualIds.push(data.id);
                    tipDialog(annualIds, "");
                    break;
                case 'edit':
                    $(".dept-table").hide();
                    $(".operation_dept").show();
                    $(".title").html("编辑机器人问题");
                    $(".operation_dept input[name=id]").val(data.id);
                    $(".operation_dept input[name=question]").val(data.question);
                    $(".operation_dept input[name=answer]").val(data.answer);
                    if (data.status == 1) {
                        $(".operation_dept input[name=status]").attr('checked', 'checked');
                        $(".operation_dept input[name=status]").attr('type', 'hidden').val(1);
                        var x = document.getElementsByClassName("layui-unselect layui-form-switch"); //获取选择框的内容
                        x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch"); //给选择框添加打开属性
                        var d = document.getElementsByTagName('em')[0];
                        d.firstChild.nodeValue = '使用';
                    } else {
                        console.log(data,333);
                        $(".operation_dept input[name=status]").attr('type', 'hidden').removeAttr("checked").val(0);
                        var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                        x[0].setAttribute("class", "layui-unselect layui-form-switch");
                        var d = document.getElementsByTagName('em')[0];
                        d.firstChild.nodeValue = '禁用';
                    }
                    form.render(); //更新全部
                    initTree(data.id);
                    break;
            }
        });
        table.on('toolbar(robotTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的机器人回答");
                    } else {
                        var annualIds = [];
                        $(data).each(function (index, item) {
                            annualIds.push(item.id);
                        });
                        tipDialog(annualIds, "选中的");
                    }
                    break;
                case 'addDept':
                    selectNode = null;
                    $(".dept-table").hide();
                    $(".operation_dept").show();
                    $(".title").html("新增机器人回答");
                    $(".operation_dept input[name=id]").val("");
                    $(".operation_dept input[name=question]").val("");
                    $(".operation_dept input[name=answer]").val("");
                    $(".operation_dept input[name=status]").attr('checked', 'checked');
                    $(".operation_dept input[name=status]").attr('type', 'hidden').val(1);
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '使用';
                    form.render(); //更新全部
                    initTree("");
                    break;
                // case uploadAnnual:
                //
                //     break;
                // case 'downloadAnnual':
                //     downloadAnnual();
                //     break;
            }
        });
        /**
         * 下载按钮调用接口
         */
        var downloadAnnual = function () {
            CoreUtil.sendPost("/sys/download", {}, function (res) {
                console.log(res);
            });
        };
        /**
         * 批量删除和单个删除统一调用接口
         * @param annualIds 选择删除的id数组
         * @param enterpriseName 选择删除的id的企业名
         */
        var tipDialog = function (annualIds, enterpriseName) {
            layer.open({
                content: '确定要删除' + enterpriseName + "机器人回答么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sys/annual", annualIds, function (res) {
                        layer.msg(res.msg);
                        search();
                    });
                }
            });
        };
        /**
         * 返回按钮
         */
        $("#btn_cancel").click(function () {
            $(".dept-table").show();
            $(".operation_dept").hide();
            return false;
        });
        /**
         * 切换按钮时修改当前的value值
         */
        form.on('switch(switch)', function () {
            $(".operation_dept input[name=enterprise_status]").attr('type', 'hidden').val(this.checked ? 1 : 2);
        });
        /**
         * 用户新增修改的提交按钮
         * 通过id是否为空判断调用不同的接口
         */
        form.on('submit(submit)', function (data) {
            console.log(data);
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/sys/annual", data.field, function (res) {
                    reloadTreeTable();
                    $(".dept-table").show();
                    $(".operation_dept").hide();
                });
            } else {
                CoreUtil.sendPut("/sys/annual", data.field, function (res) {
                    reloadTreeTable();
                    $(".dept-table").show();
                    $(".operation_dept").hide();
                });
            }

            return false;
        });
        /**
         * 刷新树形数据的接口
         * @param id
         */
        var initTree = function (id) {
            var param = {deptId: id}
            CoreUtil.sendGet("/sys/dept/tree", param, function (res) {
                loadDeptTree(res.data);
            });
        };

    });
    // /**
    //  * 设置日期
    //  */
    // laydate.render({
    //     elem: '#date_of_establishment'
    //     , type: 'date'
    //     , range: '~'
    //     , done: function (value, date, endDate) {
    //         if (value != null && value != undefined && value != "") {
    //             startDateOfEstablishment = value.split("~")[0];
    //             endDateOfEstablishment = value.split("~")[1];
    //         } else {
    //             startDateOfEstablishment = null;
    //             endDateOfEstablishment = null;
    //         }
    //
    //     }
    // });
    // laydate.render({
    //     elem: '#approval_date'
    //     , type: 'date'
    //     , range: '~'
    //     , done: function (value, date, endDate) {
    //         if (value != null && value != undefined && value != "") {
    //             startApprovalDate = value.split("~")[0];
    //             endApprovalDate = value.split("~")[1];
    //         } else {
    //             startApprovalDate = null;
    //             endApprovalDate = null;
    //         }
    //
    //     }
    // });

    /**
     * 条件查询按钮方法
     */
    function search() {
        //这里以搜索为例
        tableIns2.reload({
            where: { //设定异步数据接口的额外参数，任意设
                enterpriseName: $("#question").val(),
                socialCreditCode: $("#answer").val(),
                reportStatus: $("#status").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>
</body>
</html>